from pydantic import BaseModel, Field
from typing import List, Optional, Dict, Any
from datetime import datetime
from enum import Enum

class BookType(str, Enum):
    FAIRY_TALE = "童话故事"
    SCIENCE = "科普知识"
    HISTORY = "历史文化"
    LITERATURE = "文学名著"
    EDUCATION = "教育启蒙"
    PHILOSOPHY = "哲学思考"
    ADVENTURE = "冒险故事"
    MYTHOLOGY = "神话传说"
    FABLE = "寓言故事"

class Region(str, Enum):
    DOMESTIC = "国内"
    OVERSEAS = "海外"

class Book(BaseModel):
    id: Optional[str] = Field(None, alias="_id")
    opening_text: str
    book_name: str
    author_name: str
    book_type: BookType
    book_description: str
    region: Region
    min_age: int = Field(ge=0, le=100)
    max_age: int = Field(ge=0, le=100)
    elf_list: List[str] = Field(default_factory=list)
    created_at: Optional[datetime] = None
    
    class Config:
        populate_by_name = True

class ElfProfile(BaseModel):
    elf_id: str
    user_id: str
    basic_info: Dict[str, Any]
    personality_traits: Dict[str, float]
    value_system: Dict[str, float]
    current_emotional_state: Dict[str, Any]
    interaction_summary: Dict[str, Any]
    custom_prompts: Dict[str, Any]
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

class FeedBookRequest(BaseModel):
    elf_id: str
    book_id: str
    
class FeedBookResponse(BaseModel):
    success: bool
    message: str
    intelligence_before: int
    intelligence_after: int
    books_read: int

class DeleteBookRequest(BaseModel):
    elf_id: str
    book_id: str

class GetElfBooksResponse(BaseModel):
    elf_id: str
    books: List[Book]
    total_books: int

class ErrorResponse(BaseModel):
    error: str
    detail: Optional[str] = None